package com.appspot.gaeforum309.pathing;

import java.util.ArrayList;
import java.util.List;

import com.appspot.gaeforum309.pathing.PathBuilder.IPathable;

public class Action
{
	public enum FieldType
	{
		text,
		longtext,
		hidden,
		integer
	}
	
	public enum ActionType
	{
		createChild,
		removeNode,
	}
	
	public class Field
	{
		public String name;
		public String defaultValue;
		public FieldType type;
		public Field(String name, String defaultValue, FieldType type) {
			super();
			
			if(name == null) name = "";
			if(defaultValue == null) defaultValue = "";
			
			this.name = name;
			this.defaultValue = defaultValue;
			this.type = type;
		}
	}
	
	private List<Field> fields = new ArrayList<Field>();
	boolean isAuthorized = false;
	private ActionType actionType;
	
	private Action(IPathable object, ActionType actionType)
	{
		this.actionType = actionType;
		
		addField("gaeAction", FieldType.hidden, actionType.toString());
		addField("gaeTagName", FieldType.hidden, object.getPathable().getTagName());
		addField("gaeTagKey", FieldType.hidden, object.getKeyStr());
	}
	
	/**
	 * Test if an action is authorized for an object, and return list of information required to make action.
	 * Return null if action not authorized, otherwise return Action object.
	 * @param object Object concerned by action.
	 * @param actionType Type of action.
	 * @return null if action not authorized, of Action object with required informations.
	 */
	public static Action buildAction(IPathable object, ActionType actionType)
	{
		if(!actionAuthorized(object, actionType))
			return null;
		
		Action action = new Action(object, actionType);
		
		switch(actionType)
		{
		case createChild:
			PathableClass childType = PathBuilder.getChildType(object.getPathable().getTagName());
			childType.fillAction(actionType, action);
			return action;
		case removeNode:
			PathableClass type = object.getPathable();
			type.fillAction(actionType, action);
			return action;
		}
		
		return null;
	}
	
	public static boolean actionAuthorized(IPathable object, ActionType actionType)
	{
		// Check if creation of a child is authorized for the current object
		if(!object.authorizedAction(actionType))
			return false;
		
		switch(actionType)
		{
		case createChild:
			// Create child is possible only for objects which can have childs.
			PathableClass childType = PathBuilder.getChildType(object.getPathable().getTagName());
			if(childType == null)
				return false;
			
			return true;
		default:
			return false;
		}
	}
	
	public Field addField(String name, FieldType type, String defaultValue)
	{
		Field f = new Field(name, defaultValue, type);
		fields.add(f);
		return f;
	}
	
	public List<Field> getFieldsList()
	{
		return fields;
	}
	
	public Field getField(String name)
	{
		for(Field f:fields)
		{
			if(f.name.equals(name))
				return f;
		}
		return null;
	}
	
	public ActionType getActionType()
	{
		return actionType;
	}
	
	public static boolean apply(Action action)
	{
		switch(action.getActionType())
		{
		case createChild:
			
			break;
		}
		
		return false;
	}
}
